#pragma once

#include "db.h"
#include "feature.h"
#include <vector>
#include <string.h>
#include <iostream>

class feature_db
{
	std::string _file;
	base_feature *_feat;
	DB_ENV *_env;
	DB *_db;
	long long _cachesize;
	int _shm_key;
public:
	enum open_flag_t {read, create};
	typedef int(*traverse_callback)(DBT &key, DBT &data, void *arg);
public:
	feature_db(); 
	bool open(const std::string &file, open_flag_t flags);
	bool open(const std::string &file, const std::string &feat_type, open_flag_t flags);
	int mem_open(const std::string &db_001, const std::string &name, const std::string &feat_type); // read only
	~feature_db()
	{
		if (_db)
			_db->close(_db, 0);
		if (_feat)
			delete(_feat);
		if (_env)
			_env->close(_env, 0);
	}
	
	int set_cachesize(long long size);
	int get_feature_length() { return _feat->get_length(); }
	int append(int subid, int vid, base_feature *feat);
	int put(int subid, int vid, const std::vector<base_feature*> &feat_vec);
	int put(DBT &key, DBT &data) {return _db->put(_db, 0, &key, &data, DB_NOOVERWRITE); }
	int get(int subid, int vid, std::vector<base_feature*> &feat_vec);
	int del(int subid, int vid);
	int set(int subid, int vid, const std::vector<base_feature *> &feat_vec);
	int put_word_count(long long image_count, int subid);
	int traverse(traverse_callback on_traverse, void *arg);
	int traverse_class(int, int, int);
	int truncate_data(int, int, feature_db &);

	// new methods for v-search 4.0
	int get_vid_data(int vid, unsigned char *&data, unsigned long long &length);
	int get_total_word_count();
};
